<template>
  <!-- 订单管理 -->
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="订单号">
              <el-input v-model="form.order_sn" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名/手机号">
              <el-input v-model="form.username" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级姓名/手机号">
              <el-input v-model="form.ref_username" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单状态">
              <el-select v-model="form.order_status" placeholder="请选择" clearable>
                <el-option
                  v-for="(item,index) in statusType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单下单时间" width="236">
              <el-date-picker
                v-model="placeDates"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单支付时间" width="236">
              <el-date-picker
                v-model="payDates"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="18">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="handleExport">导出</el-button>
              <el-button type="primary" @click="toRoutes('goddessOrderAdd')">订单录入</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div class="listDiv" v-loading="tableLoading">
      <el-table ref="tableData" :data="tableData" style="width: 100%">
        <!--   -->
        <el-table-column
          fixed
          type="index"
          label="编号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="订单编号" prop="order_sn" align="center" width="200px" />
        <el-table-column label="姓名" prop="realname" align="center" />
        <el-table-column label="手机号" prop="username" align="center" width="120px">
          <template slot-scope="scope">
            <span>{{scope.row.username |phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="上级姓名" prop="ref_realname" align="center" width="120px" />
        <el-table-column label="上级手机号" prop="ref_username" align="center" width="120px">
          <template slot-scope="scope">
            <span>{{scope.row.ref_username |phonereg}}</span>
          </template>
        </el-table-column>
        <el-table-column label="购买卡项" prop="order_name" align="center" width="120px" />
        <el-table-column label="订单状态" prop="order_status" align="center" width="120px">
          <template slot-scope="scope">
            <span>{{scope.row.order_status | statusFn}}</span>
          </template>
        </el-table-column>
        <el-table-column label="定金支付方式" prop="pay_type" align="center" width="120px" />
        <el-table-column label="线上支付金额" prop="money" align="center" width="120px" />
        <el-table-column label="剩余尾款支付金额" prop="surplus_money" align="center" width="140px" />
        <el-table-column label="支付时间" prop="pay_time" align="center" width="200px" />
        <el-table-column label="尾款支付时间" prop="final_time" align="center" width="200px" />
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              v-if="scope.row.order_status == 2"
              @click="paymentCom(scope.row.id,scope.row.realname,scope.row.username,scope.row.order_name)"
            >发放分成</el-button>
            <el-button
              size="small"
              type="text"
              v-if="scope.row.order_status == 3 && scope.row.order_type != 4"
              @click="dividedEtails(scope.row.id)"
            >分成详情</el-button>
            <el-button
              size="small"
              type="text"
              v-if="scope.row.order_status == 6"
              @click="repair(scope.row.id)"
            >分成修复</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <!-- 女王卡优化分成详情 -->
    <el-dialog
      title="分成详情"
      :visible.sync="dialogFormVisible"
      width="90%"
      center
      class="dialogColor"
      @close="close"
    >
      <div class="content">
        <h5 class="titleInfo">分成详情</h5>
        <el-row>
          <el-col :span="16" style="background:white">
            <el-form
              ref="form1"
              :inline="true"
              label-position="top"
              :model="form1"
              label-width="150px"
            >
              <el-row>
                <el-col :span="8">
                  <el-form-item label="订单编号">
                    <span class="spanToInput">{{form1.listDetail.order_sn | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="购买人姓名">
                    <span class="spanToInput">{{form1.listDetail.realname | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="手机号">
                    <span class="spanToInput">{{form1.listDetail.username | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="购买数量">
                    <span class="spanToInput">{{form1.listDetail.number | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="实付金额">
                    <span class="spanToInput">{{form1.listDetail.price | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="订单生成时间">
                    <span class="spanToInput">{{form1.listDetail.created_at | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="分成发放时间">
                    <span class="spanToInput">{{form1.listDetail.final_time | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="订单状态">
                    <span class="spanToInput">{{form1.listDetail.order_type | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="分成模式">
                    <span class="spanToInput">{{form1.listDetail.divide_mode | noFn}}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div class="line"></div>
            <h5 class="titleInfo">业绩奖励</h5>
            <el-form ref="form1" :inline="true" :model="form1" label-width="150px">
              <div v-if="form1.list1.length>0">
                <el-row v-for="(item,index) in form1.list1" :key="index">
                  <el-col :span="12">
                    <el-form-item
                      :label="item.awardType==16?'会员奖励':item.awardType==1?'一级人员':item.awardType==2?'二级人员':''"
                    >
                      <span class="spanToInput">{{item.realName}}{{item.username}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="人员级别">
                      <span class="spanToInput">{{item.roleName}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励比例(%)">
                      <span class="spanToInput">{{item.rewardRate | nullFn}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励金额">
                      <span class="spanToInput">{{item.amount}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-else class="fontColor">暂无</div>
            </el-form>
            <div class="line" />
            <h5 class="titleInfo">团队奖励</h5>
            <el-form ref="form1" :inline="true" :model="form1" label-width="150px">
              <div v-if="form1.list2.length>0">
                <el-row v-for="(item,index) in form1.list2" :key="index">
                  <el-col :span="12">
                    <el-form-item label="奖励人员">
                      <span class="spanToInput">{{item.realName}}{{item.username}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="人员级别">
                      <span class="spanToInput">{{item.roleName}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励比例(%)">
                      <span class="spanToInput">{{item.rewardRate | nullFn}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励金额">
                      <span class="spanToInput">{{item.amount}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-else class="fontColor">暂无</div>
            </el-form>
            <div class="line" />
            <h5 class="titleInfo">区域奖励</h5>
            <el-form ref="form1" :inline="true" :model="form1" label-width="150px">
              <div v-if="form1.list3.length>0">
                <el-row v-for="(item,index) in form1.list3" :key="index">
                  <el-col :span="12">
                    <el-form-item label="奖励人员">
                      <span class="spanToInput">{{item.realName}}{{item.username}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="人员级别">
                      <span class="spanToInput">{{item.roleName}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励比例(%)">
                      <span class="spanToInput">{{item.rewardRate | nullFn}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励金额">
                      <span class="spanToInput">{{item.amount}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-else class="fontColor">暂无</div>
            </el-form>
          </el-col>
          <el-col :span="8">
            <div class="recommend">
              <div>当前推荐关系</div>
              <div v-if="userList.length>0">
                <div class="box" v-for="(item,index) in userList" :key="index">
                  <div class="boxContent">{{item}}</div>
                  <div class="arrow" v-if="index!=userList.length-1">↓</div>
                </div>
              </div>
              <div v-else class="mb_20">暂无</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-dialog>
    <!-- 分成详情弹窗 -->
    <!-- <el-dialog title="发放详情" :visible.sync="dividedDialogVisible" width="50%" center>
      <div v-loading="tableLoading1">
        <el-table ref="tableData1" :data="tableData1" style="width: 100%" border>
          <el-table-column label="推荐人" prop="realname" align="center" />
          <el-table-column label="推荐人手机号" prop="username" align="center" />
          <el-table-column label="推荐人级别" prop="role_name" align="center" />
          <el-table-column label="分成金额" prop="money" align="center" />
        </el-table>
      </div>
    </el-dialog>-->
  </div>
</template>

<script>
import {
  getOrder,
  getPayment,
  orderDivided,
  orderExport,
  orderDetail,
  orderRepair
} from "@/api/goddessMange";
import { userChain, layeredDetail } from "@/api/goddessJava";
import commonFn from "@/utils/common";
export default {
  name: "goddessOrder",
  data() {
    return {
      form: {
        order_sn: "",
        username: "",
        ref_username: "",
        order_status: "",
        created_at_start: "",
        created_at_end: "",
        pay_time_start: "",
        pay_time_end: "",
        page: 1,
        pagesize: 10
      },
      form1: {
        list1: [],
        list2: [],
        list3: [],
        listDetail: {}
      },
      userList: [],
      dividedInfo: [],
      type: "",
      placeDates: null,
      payDates: null,
      // 分页
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0, // 总条目数,
      statusType: [
        // { name: "待支付", value: 1 },
        { name: "待审核", value: 2 },
        { name: "订单完成", value: 3 },
        { name: "已退款", value: 4 },
        { name: "支付超时", value: 5 }
      ],
      tableData: [],
      tableData1: [], //分成详情
      tableLoading: true,
      tableLoading1: true,
      dividedDialogVisible: false, //分成
      dialogFormVisible: false //女王卡优化分成
    };
  },
  filters: {
    // 订单状态
    statusFn(opt) {
      switch (opt) {
        case 1:
          return "待支付";
          break;
        case 2:
          return "待审核";
          break;
        case 3:
          return "订单完成";
          break;
        case 4:
          return "已退款";
          break;
        case 5:
          return "支付超时";
          break;
        case 6:
          return "分成异常";
          break;
        case 7:
          return "待补齐尾款";
          break;
        default:
          // return "暂无";
          break;
      }
    },
    nullFn(opt) {
      if (opt) {
        return opt;
      } else {
        return "--";
      }
    },
    // 无数据显示
    noFn(opt) {
      if (opt) {
        return opt;
      } else {
        return "";
      }
    }
  },
  created() {
    this.getList(this.form);
  },
  methods: {
    toRoutes(name) {
      let route = { name };
      this.$router.push(route);
    },
    // 序号
    table_index(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1;
    },
    // 获取列表
    getList(e) {
      getOrder(e)
        .then(res => {
          if (res.data.code == 200) {
            this.tableData = res.data.data;
            this.page_totals = res.data.total;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
          this.tableLoading = false;
        })
        .catch(err => {
          this.errorStatus(err.msg);
          this.tableLoading = false;
        });
    },
    // 审核发放
    paymentCom(id, name, phone, card) {
      let datas = {
        id,
        operator_id: localStorage.getItem("userId"),
        operator_name: localStorage.getItem("username")
      };
      this.$confirm(
        "确认" + name + "用户" + phone + "手机号的" + card + "分成进行发放",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          getPayment(datas).then(res => {
            if (res.data.code == 200) {
              this.getList(this.form);
            } else {
              this.$message({
                type: "info",
                message: res.data.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 订单异常处理
    repair(id) {
      let data = {
        id,
        operator_id: localStorage.getItem("userId"),
        operator_name: localStorage.getItem("username")
      };
      this.$confirm("确认是否处理", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          orderRepair(data).then(res => {
            if (res.data.code == 200) {
              this.$message({
                message: "异常处理成功",
                type: "success"
              });
              this.getList(this.form);
            } else {
              this.$message({
                type: "info",
                message: res.data.msg
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消"
          });
        });
    },
    // 分成详情
    getDetail(id) {
      let data = { id };
      orderDetail(data)
        .then(res => {
          if (res.data.code == 200) {
            this.form1.listDetail = res.data.data;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err.msg);
        });
    },
    // 分成详情调用事件
    dividedEtails(id) {
      this.dialogFormVisible = true;
      let data = {
        orderId: id,
        type: 8
      };
      this.getDetail(id);
      this.getUser(data);
      this.detailInto(data);
    },
    //获取分成业绩，团队，区域等奖励信息
    detailInto(data) {
      layeredDetail(data)
        .then(res => {
          if (res.data.code == 0) {
            let arr = [];
            let arr1 = [];
            let arr2 = [];
            if (res.data.data) {
              res.data.data.forEach(item => {
                if (
                  item.awardType == 16 ||
                  item.awardType == 1 ||
                  item.awardType == 2
                ) {
                  arr.push(item);
                }
                if (item.awardType == 3) {
                  arr1.push(item);
                }
                if (item.awardType == 8) {
                  arr2.push(item);
                }
                this.form1.list1 = arr;
                this.form1.list2 = arr1;
                this.form1.list3 = arr2;
              });
            } else {
              this.form1.list1 = [];
              this.form1.list2 = [];
              this.form1.list3 = [];
            }
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err);
        });
    },
    // 获取分成用户链
    getUser(data) {
      userChain(data)
        .then(res => {
          if (res.data.code == 0) {
            this.userList = res.data.data;
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(err => {
          this.errorStatus(err);
        });
    },
    // 分成弹窗关闭事件
    close() {
      this.dialogFormVisible = false;
      this.form1 = {
        list1: [],
        list2: [],
        list3: [],
        listDetail: {}
      };
      this.userList = [];
    },
    // 分成详情
    // dividedEtails(id) {
    //   this.dividedDialogVisible = true;
    //   // 女王卡优化分成
    //   let datas = { id };
    //   orderDivided(datas)
    //     .then(res => {
    //       if (res.data.code == 200) {
    //         this.tableData1 = res.data.data;
    //       } else {
    //         this.$message({
    //           showClose: true,
    //           message: res.data.msg,
    //           type: "warning"
    //         });
    //       }
    //       this.tableLoading1 = false;
    //     })
    //     .catch(err => {
    //       this.errorStatus(err.msg);
    //       this.tableLoading1 = false;
    //     });
    // },
    // 查询
    handleSearch() {
      this.form.page = 1;
      if (this.placeDates != null) {
        if (this.placeDates.length > 0) {
          this.form.created_at_start = this.placeDates[0];
          this.form.created_at_end = this.placeDates[1];
        } else {
          this.form.created_at_start = null;
          this.form.created_at_end = null;
        }
      } else {
        this.form.created_at_start = null;
        this.form.created_at_end = null;
      }

      if (this.payDates != null) {
        if (this.payDates.length > 0) {
          this.form.pay_time_start = this.payDates[0];
          this.form.pay_time_end = this.payDates[1];
        } else {
          this.form.pay_time_start = null;
          this.form.pay_time_end = null;
        }
      } else {
        this.form.pay_time_start = null;
        this.form.pay_time_end = null;
      }
      this.getList(this.form);
    },

    // 导出
    handleExport() {
      let datas = { ...this.form };
      delete datas.page;
      delete datas.pagesize;
      orderExport(datas)
        .then(res => {
          commonFn.downFile("女王卡订单导出", res);
        })
        .catch(err => {
          this.errorStatus(err.msg);
        });
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.getList(this.form);
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val;
      this.getList(this.form);
    },
    // // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      });
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, "error");
    }
  }
};
</script>
<style scoped>
.app-container {
  padding: 0px !important;
  background: transparent;
}
.el-range-editor--medium.el-input__inner {
  width: 236px;
}
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
.allCenter {
  text-align: center;
}
.spanToInput {
  width: 205px;
  min-height: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
  border: 1px solid #ddd;
  text-align: left;
  padding: 0 12px;
}
.line {
  height: 25px;
  background: #f7f7f7;
}
.recommend {
  min-width: 280px;
  color: #1d2023;
  text-align: center;
  font-size: 16px;
  margin-left: 25px;
}
.boxContent {
  margin-top: 20px;
  height: 25px;
  line-height: 25px;
  border: 1px solid #ddd;
  text-align: center;
  padding: 0 12px;
}
.arrow {
  text-align: center;
  padding: 12px 0 0 0;
}
.fontColor {
  font-size: 16px;
  text-align: center;
  margin-bottom: 15px;
}
.mb_20 {
  font-size: 20px;
  text-align: center;
  margin-top: 20px;
}
</style>